package com.ly.lw.styledo.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSONObject;
import com.etsy.android.grid.StaggeredGridView;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.bitmap.PauseOnScrollListener;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.ly.lw.styledo.R;
import com.ly.lw.styledo.adapter.AutoImagePageAdapter;
import com.ly.lw.styledo.adapter.MainAdapter;
import com.ly.lw.styledo.adapter.ViewPagerAdapter;
import com.ly.lw.styledo.bean.Home;
import com.ly.lw.styledo.bean.Normal;
import com.ly.lw.styledo.bean.PlanList;
import com.ly.lw.styledo.bean.ResultJson;
import com.ly.lw.styledo.config.UrlConstants;
import com.ly.lw.styledo.ui.MainDetail;
import com.ly.lw.styledo.ui.MainDetailScro;
import com.ly.lw.styledo.utils.FileUtils;
import com.ly.lw.styledo.widget.AutoScrollViewPager;

import java.util.ArrayList;
import java.util.List;


public class MainFragment extends Fragment implements AdapterView.OnItemClickListener {
    @ViewInject(R.id.grid_view)
    private StaggeredGridView staggeredGridView;
    private MainAdapter mainAdapter;
    private ArrayList<Normal> listNormals = new ArrayList<>();


    private BitmapUtils bitmapUtils;
    private HeadHolder holder;
    private LayoutInflater inflater;


    /**
     * 广告栏
     *
     * @param savedInstanceState
     */
    private ArrayList<View> rotationViews = new ArrayList<>();
    private ViewPagerAdapter viewPagerAdapter;
    private AutoImagePageAdapter autoImagePageAdapter;
    private List<PlanList> planLists = new ArrayList<>();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBitmap();

    }

    private void initBitmap() {
        bitmapUtils = new BitmapUtils(getActivity().getApplicationContext(),FileUtils.getImageCache());
        //bitmapUtils.configDefaultLoadFailedImage(R.mipmap.ic_default_item);
       // bitmapUtils.configDefaultLoadingImage(R.color.white);

        AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
        animation.setDuration(1000);
        bitmapUtils.configDefaultImageLoadAnimation(animation);
        bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);//设置图片压缩类型

        bitmapUtils.configMemoryCacheEnabled(true);
        bitmapUtils.configDiskCacheEnabled(true);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        this.inflater = inflater;
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        ViewUtils.inject(this, view);
        initView();
        loadData();
        return view;
    }


    private void loadData() {
        HttpUtils httpUtils = new HttpUtils();
        httpUtils.send(HttpRequest.HttpMethod.POST, UrlConstants.HOME_URL, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                //获取服务器返回的json数据
                String result = responseInfo.result;
                Home home = JSONObject.parseObject(result, Home.class);
                if (home != null) {

                    ResultJson resultJson = home.getResult();

                    if (resultJson != null) {
                        List<Normal> normals = resultJson.getNormals();
                        if (normals != null && normals.size() > 0) {
                            listNormals.addAll(normals);
                            mainAdapter.notifyDataSetChanged();
                        }

                        List<PlanList> planList = resultJson.getPlan_list();
                        if (planList != null && planList.size() > 0) {
                            //refreshRotation(planList);
                            planLists.addAll(planList);
                            autoImagePageAdapter.notifyDataSetChanged();

                        }


                    }


                }


            }

            @Override
            public void onFailure(HttpException e, String s) {


            }
        });

    }

    private void refreshRotation(List<PlanList> planList) {
        for (int i = 0; i < planList.size(); i++) {
            ImageView img = new ImageView(getActivity());
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            img.setLayoutParams(params);
            img.setScaleType(ImageView.ScaleType.FIT_XY);
            bitmapUtils.display(img, planList.get(i).getBanner_img());
            rotationViews.add(img);
        }
        viewPagerAdapter.notifyDataSetChanged();
    }

    private void initView() {
        mainAdapter = new MainAdapter(listNormals, getActivity(), bitmapUtils);
        View headView = createHeadView();
        staggeredGridView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true));
        staggeredGridView.setOnItemClickListener(this);
        staggeredGridView.addHeaderView(headView);

        staggeredGridView.setAdapter(mainAdapter);


    }

    private View createHeadView() {
        View headView = inflater.inflate(R.layout.item_head_layout, staggeredGridView, false);
        holder = new HeadHolder(headView);
       // viewPagerAdapter = new ViewPagerAdapter(rotationViews);
        autoImagePageAdapter = new AutoImagePageAdapter(planLists,getActivity(),bitmapUtils);
        holder.headPager.setAdapter(autoImagePageAdapter);
        return headView;
    }

    /**
     * 为主页每一个item设置监听
     *
     * @param parent
     * @param view
     * @param position
     * @param id
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), MainDetailScro.class);
        startActivity(intent);

    }

    public class HeadHolder {
        @ViewInject(R.id.item_head_viewpager)
        AutoScrollViewPager headPager;

        public HeadHolder(View view) {
            ViewUtils.inject(this, view);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        holder.headPager.startAutoScroll();
    }


    @Override
    public void onPause() {
        super.onPause();
        holder.headPager.stopAutoScroll();
    }


}
